package com.example.producer.controller;

import com.example.common.entity.MsgTest;
import com.example.rocket.core.NbCallback;
import com.example.rocket.core.NbSendResult;
import com.example.rocket.producer.NbMqProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @description: TODO
 * @author TAO
 * @date 2021/12/21 10:50 上午
 */
@Slf4j
@RestController
public class MqTestController {

    @Resource
    private NbMqProducer producer;

    @GetMapping("/syncSend")
    public String syncSend() {
        producer.syncSend("first-topic-str", null, "Hello World syncSend");
        return "同步消息发送成功";
    }

    @GetMapping("/syncSend2")
    public String syncSend2() {
        MsgTest msg = new MsgTest(1, "hello world test3", new Date());
        String msgId = producer.syncSend("first-topic-nb-pojo", null, msg);
        return "同步消息发送成功";
    }


    @GetMapping("/sendOneWay")
    public String sendOneWay() {
        producer.sendOneWay("first-topic-str", null, "Hello World sendOneWay");
        return "单向消息发送成功";
    }

    @GetMapping("/asyncSend")
    public String asyncSend() {
        producer.asyncSend("first-topic-str", null, "Hello World asyncSend", new NbCallback() {
            @Override
            public void onSuccess(String sendResult) {
                log.info("外部调用成功返回===>{}",sendResult);
            }
            @Override
            public void onException(Throwable e) {
                log.info("外部调用失败返异常===>{}",e);
            }
        });
        return "异步消息发送成功";
    }


    /**
     * 同步延迟消息
     */
    @GetMapping("/syncSendDelayedStr")
    public String  syncSendDelayedStr() {
        NbSendResult res = producer.syncSend("first-topic-str", null, "1111", 3000, 4);
        log.info("res==>{}", res);
        return res.toString();
    }

    /**
     * 异步延迟消息
     * @throws InterruptedException
     */
    @GetMapping("/asyncSendDelayedStr")
    public String asyncSendDelayedStr() throws InterruptedException {
        producer.asyncSend("first-topic-str", null, "1111", 3000, 4, new NbCallback() {
            @Override
            public void onSuccess(String sendResult) {
                log.info("外部调用成功返回===>{}", sendResult);
            }

            @Override
            public void onException(Throwable e) {
                log.info("外部调用失败返异常===>{}", e);
            }
        });
        Thread.sleep(3000);
        return "异步延迟消息发送成功";
    }


}
